<?php

namespace web\api\controller;

/**
 * Description of Addr
 * 用户地址
 * @author shilinqing
 */
class Addr extends UserBase{
    
    /**
     * 获取地址
     * @return type
     */
    public function getAddrList(){
        try{
            $filter = 'user_id='.$this->user_id;
            $m = new \addons\shop\model\MemberAddress();
            $data = $m->getDataList(-1,-1,$filter);
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    
    /**
     * 修改地址
     * @return type
     */
    public function postAddr(){
        if(IS_POST){
            try{
                $data['id'] = $this->_post('id');
                $data['address'] = $this->getParamVal('address');
                $data['province'] = $this->getParamVal('province');
                $data['city'] = $this->getParamVal('city');
                $data['area'] = $this->getParamVal('area');
                $data['name'] = $this->getParamVal('name');
                $data['area_id'] = $this->getParamVal('area_id');
                $data['city_id'] = $this->getParamVal('city_id');
                $data['province_id'] = $this->getParamVal('province_id');
//                $data['sex'] = $this->getParamVal('sex');
                // $data['country_code'] = $this->getParamVal('country_code');
                $data['mobile'] = $this->getParamVal('mobile');
                $data['is_default'] = $this->getParamVal('is_default');
                if($data['is_default'] != 1){
                    $data['is_defualt'] = 0;
                }
                $data['user_id'] = $this->user_id;
                $data['update_time'] = NOW_DATETIME;
                $m = new \addons\shop\model\MemberAddress();
                if(empty($data['id'])){
                    $id = $m->add($data);
                }else{
                    $m->save($data);
                    $id = $data['id'];
                }
                if($data['is_default'] == 1){
                    $m->setAnotherDefault($id,$this->user_id);
                }
                return $this->successJSON();
            } catch (\Exception $ex) {
                return $this->failJSON($ex->getMessage());
            }
        }else{
            return $this->failJSON(lang('COMMON_MSG'));
        }
    }
    
    /**
     * 删除地址
     * @return type
     */
    public function delAddr(){
        try{
            $id = $this->getParamVal('id');
            $m = new \addons\shop\model\MemberAddress();
            $ret = $m->delUserAddr($id,$this->user_id);
            if($ret > 0){
                return $this->successJSON();
            }else{
                return $this->failJSON(lang('ADDR_DEL_FAIL'));
            }
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 根据id获取地址
     * @return type
     */
    public function addrDetail(){
        try{
            $id = $this->getParamVal('id');
            $m = new \addons\shop\model\MemberAddress();
            $where['id'] = $id;
            $where['user_id'] = $this->user_id;
            $data = $m->where($where)->find();
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    /**
     * 获取默认地址
     * @return type
     */
    public function getDefaultAddr(){
        $m = new \addons\shop\model\MemberAddress();
        $where['is_default'] = 1;
        $where['user_id'] = $this->user_id;
        $data = $m->where($where)->find();
        return $this->successJSON($data);
    }
    
    /**
     * 设置默认地址
     * @return type
     */
    public function setDefaultAddr(){
        try{
            $id = $this->getParamVal('id');
            $m = new \addons\shop\model\MemberAddress();
            $ret = $m->setDefault($id, $this->user_id);
            if($ret > 0){
                $m->setAnotherDefault($id,$this->user_id);
            }
            return $this->successJSON();
            
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }
    
}
